﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

// std headers

// qt headers

// gcmp headers

// custom headers
#include "ParameterNameValidator.h"
#include "TranslatorManager.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

namespace gmepgcs
{
    ParameterNameValidator::ParameterNameValidator(QObject * parent) :
        QValidator(parent),
        m_message()
    {

    }

    ParameterNameValidator::~ParameterNameValidator()
    {

    }

    QValidator::State ParameterNameValidator::validate(QString &input, int &pos) const
    {
        auto adjustedInput = input.trimmed();

        if (adjustedInput.isEmpty())
        {
            m_message = QString::fromStdWString(GBMP_TR(L"名称不能为空！"));
            return State::Invalid;
        }

        if (QDoubleValidator().validate(adjustedInput, pos) == State::Acceptable)
        {
            m_message = QString::fromStdWString(GBMP_TR(L"名称不能为纯数字或包含运算符！"));
            return State::Invalid;
        }

        QStringList illegalChars = {"+", "-", "*", "/", "\\", "(", ")", "（", "）", "=" };
        for (auto &illegalChar : illegalChars)
        {
            if (adjustedInput.contains(illegalChar, Qt::CaseInsensitive))
            {
                m_message = QString::fromStdWString(GBMP_TR(L"名称不能为纯数字或包含运算符！"));
                return State::Invalid;
            }
        }

        return State::Acceptable;
    }

    QValidator::State ParameterNameValidator::Validate(const QString & input, QString & message)
    {
        int pos;
        auto state = validate(QString(input), pos);
        message = m_message;

        return state;
    }

}